//
//  AppDelegate.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/8.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "AppDelegate.h"
#import "mainTabBarViewController.h"
#import "TLChengYu.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // w8jGc3CnVDtq46G5ec53qBgSrPVObvOv
    // 要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc] init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"w8jGc3CnVDtq46G5ec53qBgSrPVObvOv"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.navigationController.view];
    self.window.rootViewController = [[mainTabBarViewController alloc] init];
    [self.window makeKeyAndVisible];
    
    
    /********************************* Test ************************************************/
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 初始化数据,写入沙盒
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *path = [cachePath stringByAppendingPathComponent:@"cyLexicon.plist"];
        NSArray *data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        if (data == nil) {
            data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cyLexicon.plist" ofType:nil]];
            NSMutableArray *cyLexicon = [NSMutableArray array];
            for (NSArray *arr in data) {
                NSMutableArray *cys = [NSMutableArray array];
                for (NSDictionary *dict in arr) {
                    TLChengYu *cy = [TLChengYu chengYuWithDict:dict];
                    [cys addObject:cy];
                }
                [cyLexicon addObject:cys];
            }
            [NSKeyedArchiver archiveRootObject:cyLexicon toFile:path];
        }
    });
    
    return YES;
}

- (void)test {
    NSMutableArray *cys = [NSMutableArray array];
    for (NSInteger i = 2; i < 3945; i++) { // 3945
        NSString *path = nil;
        if (i < 4) {
            path = [NSString stringWithFormat:@"/Users/loong/Desktop/成语词库/%ld.txt",i];
        }else {
            path = [NSString stringWithFormat:@"/Users/loong/Desktop/成语词库/%ld",i];
        }
        
        //        NSLog(@"path:%@",path);
        NSError *error = nil;
        NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSString *subStr = [[content componentsSeparatedByString:@"<B>"] lastObject];
        NSArray *subStrs = [subStr componentsSeparatedByString:@"</B>"];
        dict[@"name"] = [subStrs firstObject];
        subStrs = [subStrs.lastObject componentsSeparatedByString:@"<h4>"];
        
        if (subStrs.count == 4)  {
            NSString *str = [subStrs objectAtIndex:1];
            dict[@"dianGu"] = [[str componentsSeparatedByString:@"</h4>"] firstObject];
            str = [subStrs objectAtIndex:2];
            dict[@"zhuShi"] = [[str componentsSeparatedByString:@"</h4>"] firstObject];
            str = [subStrs objectAtIndex:3];
            dict[@"pinYin"] = [[str componentsSeparatedByString:@"</h4>"] firstObject];
            dict[@"liJu"] = [NSString stringWithFormat:@"原来用成语“%@”可以这么造句的，妈妈再也不用当心我不会造句了？",dict[@"name"]];
            //            NSLog(@"name:%@",dict[@"name"]);
            //            NSLog(@"dianGu:%@",dict[@"dianGu"]);
            //            NSLog(@"zhuShi:%@",dict[@"zhuShi"]);
            //            NSLog(@"pinYin:%@\n\n",dict[@"pinYin"]);
        }
        
        
        [cys addObject:dict];
        
    }
    NSString *file = @"/Users/loong/Desktop/data.plist";
    
    [cys writeToFile:file atomically:YES];
    NSLog(@"%ld",cys.count);

}

- (void)setup:(NSArray *)arr {
   
    NSMutableArray *A = [NSMutableArray arrayWithArray:arr[0][@"A"]];
    NSMutableArray *B = [NSMutableArray arrayWithArray:arr[1][@"B"]];
    NSMutableArray *C = [NSMutableArray arrayWithArray:arr[2][@"C"]];
    NSMutableArray *D = [NSMutableArray arrayWithArray:arr[3][@"D"]];
    NSMutableArray *E = [NSMutableArray arrayWithArray:arr[4][@"E"]];
    NSMutableArray *F = [NSMutableArray arrayWithArray:arr[5][@"F"]];
    NSMutableArray *G = [NSMutableArray arrayWithArray:arr[6][@"G"]];
    NSMutableArray *H = [NSMutableArray arrayWithArray:arr[7][@"H"]];
//    NSMutableArray *I = [NSMutableArray arrayWithArray:arr[8][@"J"]];
    NSMutableArray *J = [NSMutableArray arrayWithArray:arr[8][@"J"]];
    NSMutableArray *K = [NSMutableArray arrayWithArray:arr[9][@"K"]];
    NSMutableArray *L = [NSMutableArray arrayWithArray:arr[10][@"L"]];
    NSMutableArray *M = [NSMutableArray arrayWithArray:arr[11][@"M"]];
    NSMutableArray *N = [NSMutableArray arrayWithArray:arr[12][@"N"]];
    NSMutableArray *O = [NSMutableArray arrayWithArray:arr[13][@"O"]];
    NSMutableArray *P = [NSMutableArray arrayWithArray:arr[14][@"P"]];
    NSMutableArray *Q = [NSMutableArray arrayWithArray:arr[15][@"Q"]];
    NSMutableArray *R = [NSMutableArray arrayWithArray:arr[16][@"R"]];
    NSMutableArray *S = [NSMutableArray arrayWithArray:arr[17][@"S"]];
    NSMutableArray *T = [NSMutableArray arrayWithArray:arr[18][@"T"]];
//    NSMutableArray *U = [NSMutableArray arrayWithArray:arr[0][@"A"]];
//    NSMutableArray *V = [NSMutableArray arrayWithArray:arr[0][@"A"]];
    NSMutableArray *W = [NSMutableArray arrayWithArray:arr[19][@"W"]];
    NSMutableArray *X = [NSMutableArray arrayWithArray:arr[20][@"X"]];
    NSMutableArray *Y = [NSMutableArray arrayWithArray:arr[21][@"Y"]];
    NSMutableArray *Z = [NSMutableArray arrayWithArray:arr[22][@"Z"]];
    NSArray *cyArr = @[A, B, C, D, E, F, G, H, J, K, L, M, N, O, P, Q, R, S, T, W, X, Y, Z];
    // Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz
//    for (NSDictionary* dict in arr) {
//        NSMutableDictionary *cy = [NSMutableDictionary dictionaryWithDictionary:dict];
//        NSString *pinyin =dict[@"pinYin"];
//        
//        NSString *c = [[pinyin substringWithRange:NSMakeRange(1, 1)] uppercaseString];
//        
//        if([c isEqual: @"Ā"] || [c isEqual: @"À"] ) {
//            c = @"A";
//        }
//        else if([c isEqual: @"Ē"] || [c isEqual: @"É"] || [c isEqual: @"Ě"] || [c isEqual: @"È"]){
//            c = @"E";
//        }else if ([c isEqualToString:@"Ǒ"]){
//            c = @"O";
//        }
//        cy[@"love"] = @(NO);
//        cy[@"index"] = c;
//        //NSLog(@" index = %@ ,name = %@",c,cy[@"pinYin"]);
//        char Char = [c characterAtIndex:0];
//        switch (Char) {
//            case 'A':
//                [A addObject:cy];
//                break;
//            case 'B':
//                [B addObject:cy];
//                break;
//            case 'C':
//                [C addObject:cy];
//                break;
//            case 'D':
//                [D addObject:cy];
//                break;
//            case 'E':
//                [E addObject:cy];
//                break;
//            case 'F':
//                [F addObject:cy];
//                break;
//            case 'G':
//                [G addObject:cy];
//                break;
//            case 'H':
//                [H addObject:cy];
//                break;
//            case 'I':
//                [I addObject:cy];
//                break;
//            case 'J':
//                [J addObject:cy];
//                break;
//            case 'K':
//                [K addObject:cy];
//                break;
//            case 'L':
//                [L addObject:cy];
//                break;
//            case 'M':
//                [M addObject:cy];
//                break;
//            case 'N':
//                [N addObject:cy];
//                break;
//            case 'O':
//                [O addObject:cy];
//                break;
//            case 'P':
//                [P addObject:cy];
//                break;
//            case 'Q':
//                [Q addObject:cy];
//                break;
//            case 'R':
//                [R addObject:cy];
//                break;
//            case 'S':
//                [S addObject:cy];
//                break;
//            case 'T':
//                [T addObject:cy];
//                break;
//            case 'U':
//                [U addObject:cy];
//                break;
//            case 'V':
//                [V addObject:cy];
//                break;
//            case 'W':
//                [W addObject:cy];
//                break;
//            case 'X':
//                [X addObject:cy];
//                break;
//            case 'Y':
//                [Y addObject:cy];
//                break;
//            case 'Z':
//                [Z addObject:cy];
//                break;
//            default:
//                NSLog(@"%@",cy[@"name"]);
//                break;
//        }
//    }
   


  NSString *file = @"/Users/loong/Desktop/data.plist";
  [cyArr writeToFile:file atomically:YES];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
